/**
 * @description
 * @Author Sun Yz
 * @Date 2024/12/26 11:24
 */

export function getDefaultContextMenu(star3d, viewer) {
  const Cesium = star3d.getLib('Cesium')
  // 绕点飞行
  const contextMenu = viewer.contextMenu
  const keyboardRoaming = new star3d.KeyboardRoaming(viewer)
  const measure = new star3d.Measure(viewer)
  const weather = new star3d.Weather(viewer)
  const plot = new star3d.Plot(viewer)
  const effect = new star3d.Effect(viewer)
  return [
    {
      // eslint-disable-next-line quotes
      label: '初始视图',
      callback: function(e) {
        viewer && viewer.flyHome()
      }
    },
    {
      label: '23维切换',
      children: [
        {
          label: '切换至2维',
          show: function(e) {
            return viewer.scene.mode !== 2
          },
          callback: function(e) {
            viewer.changeSceneMode(2)
          }
        },
        {
          label: '切换至2.5维',
          show: function(e) {
            return viewer.scene.mode !== 1
          },
          callback: function(e) {
            viewer.changeSceneMode(2.5)
          }
        },
        {
          label: '切换至3维',
          show: function(e) {
            return viewer.scene.mode !== 3
          },
          callback: function(e) {
            viewer.changeSceneMode(3)
          }
        }
      ]
    },
    {
      label: '视角切换',
      show: function(e) {
        return Cesium.defined(e.surfacePosition)
      },
      children: [
        {
          label: '绕此处环绕飞行',
          show: function(e) {
            return e.wgs84SurfacePosition && (!contextMenu.aroundView || !contextMenu.aroundView.isStart)
          },
          callback: function(e) {
            const mpt = e.wgs84SurfacePosition
            const alt = viewer.cameraPosition ? viewer.cameraPosition.alt : 500
            const heading = viewer.cameraPosition ? viewer.cameraPosition.heading : 0
            viewer.flyToPosition(new star3d.Position(mpt.lng, mpt.lat, alt, heading, -90), () => {
              contextMenu.aroundView.start()
            })
          }
        },
        {
          label: '关闭环绕飞行',
          show: function(e) {
            return contextMenu.aroundView?.isStart
          },
          callback: function(e) {
            if (contextMenu.aroundView) {
              contextMenu.aroundView.stop()
            }
          }
        },
        {
          label: '移动到此处',
          show: function(e) {
            return Cesium.defined(e.surfacePosition)
          },
          callback: function(e) {
            const mpt = e.wgs84SurfacePosition
            mpt.alt = 500
            mpt.heading = 0
            mpt.pitch = -90
            viewer.flyToPosition(
              mpt,
              () => {
                console.log('飞行完成!')
              },
              2
            )
          }
        },
        {
          label: '第一视角站到此处',
          show: function(e) {
            return Cesium.defined(e.wgs84SurfacePosition)
          },
          callback: function(e) {
            const mpt = e.wgs84SurfacePosition
            mpt.alt = 500
            mpt.heading = viewer?.cameraPosition?.heading
            mpt.pitch = 0.0
            mpt.roll = 0.0
            viewer.flyToPosition(
              mpt,
              () => {
                console.log('第一视角站立完成!')
              },
              2
            )
          }
        },
        {
          label: '开启键盘漫游',
          show: function(e) {
            return !keyboardRoaming.enable
          },
          callback: function(e) {
            keyboardRoaming.enable = true
          }
        },
        {
          label: '关闭键盘漫游',
          show: function(e) {
            return keyboardRoaming.enable
          },
          callback: function(e) {
            keyboardRoaming.enable = false
          }
        },
        {
          label: '取消锁定',
          show: function(e) {
            // viewer.delegate 即为 Cesium.Viewer对象
            return viewer.delegate.trackedEntity !== undefined
          },
          callback: function(e) {
            viewer.delegate.trackedEntity = undefined
          }
        }
      ]
    },
    {
      label: '图上量算',
      show: function(e) {
        return Cesium.defined(e.surfacePosition)
      },
      children: [
        {
          label: '空间测距',
          callback: function(e) {
            measure.distance()
          }
        },
        {
          label: '贴地测距',
          callback: function(e) {
            measure.distanceSurface()
          }
        },
        {
          label: '空间测面',
          callback: function(e) {
            measure.area()
          }
        },
        {
          label: '贴地测面',
          callback: function(e) {
            measure.areaSurface()
          }
        },
        {
          label: '高度差',
          callback: function(e) {
            measure.height()
          }
        },
        {
          label: '贴物高度差',
          callback: function(e) {
            measure.height({
              clampToModel: true
            })
          }
        },
        {
          label: '角度',
          callback: function(e) {
            measure.angle()
          }
        },
        {
          label: '贴物角度',
          callback: function(e) {
            measure.angle({
              clampToModel: true
            })
          }
        },
        {
          label: '偏航角',
          callback: function(e) {
            measure.heading()
          }
        },
        {
          label: '三角测量',
          callback: function(e) {
            measure.triangleHeight()
          }
        },
        {
          label: '删除测量',
          show: function(e) {
            return measure && measure.hasMeasure
          },
          callback: function(e) {
            if (measure) {
              measure.deactivate()
            }
          }
        }
      ]
    }
  ]
}
